package com.lechuang.system.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lechuang.common.request.PageRequest;
import com.lechuang.common.result.PageResult;
import com.lechuang.system.dto.TenantSaveDTO;
import com.lechuang.system.dto.TenantListDTO;
import com.lechuang.system.entity.TenantInfo;
import com.lechuang.system.manager.TenantManager;
import com.lechuang.system.service.TenantInfoService;
import com.lechuang.system.vo.TenantDetailVO;
import com.lechuang.system.vo.TenantListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TenantManagerImpl implements TenantManager {

    @Resource
    private TenantInfoService tenantInfoService;

    @Override
    public PageResult<TenantListVO> pageList(PageRequest<TenantListDTO> pageRequest) {
        Page<TenantInfo> pageList = tenantInfoService.pageList(pageRequest);
        List<TenantInfo> records = pageList.getRecords();
        List<TenantListVO> voList = new ArrayList<>();
        records.forEach(bean -> {
            TenantListVO vo = BeanUtil.copyProperties(bean, TenantListVO.class);
            voList.add(vo);
        });
        return PageResult.result(voList, pageList, pageRequest, null);
    }

    @Override
    public void saveAndUpdate(TenantSaveDTO tenantSaveDTO) {
        Long id = tenantSaveDTO.getId();
        if(id == null){
            this.save(tenantSaveDTO);
        }else{
            this.update(tenantSaveDTO);
        }
    }

    @Override
    public TenantDetailVO detail(Long id) {
        TenantInfo tenantInfo = tenantInfoService.getById(id);
        return BeanUtil.copyProperties(tenantInfo, TenantDetailVO.class);
    }

    @Override
    public List<TenantListVO> getByNameOrPhone(String selectText) {
        List<TenantInfo> tenantInfoList = tenantInfoService.getByNameOrPhone(selectText);
        return BeanUtil.copyToList(tenantInfoList, TenantListVO.class);
    }

    private void update(TenantSaveDTO tenantSaveDTO) {
        TenantInfo tenantInfo = new TenantInfo()
                .setName(tenantSaveDTO.getName())
                .setAddress(tenantSaveDTO.getAddress())
                .setContacts(tenantSaveDTO.getContacts())
                .setPhone(tenantSaveDTO.getPhone())
                .setRemark(tenantSaveDTO.getRemark())
                .setTenantStatus(tenantSaveDTO.getTenantStatus());
        tenantInfo.setId(tenantSaveDTO.getId());
        tenantInfoService.updateById(tenantInfo);
    }

    private void save(TenantSaveDTO tenantSaveDTO) {
        TenantInfo tenantInfo = new TenantInfo()
                .setName(tenantSaveDTO.getName())
                .setAddress(tenantSaveDTO.getAddress())
                .setContacts(tenantSaveDTO.getContacts())
                .setPhone(tenantSaveDTO.getPhone())
                .setRemark(tenantSaveDTO.getRemark());
        tenantInfoService.save(tenantInfo);
    }
}
